package com.java.test.user.onlinemembermanagepush;

import com.java.test.dto.ResponseDTO;
import com.java.test.user.onlinemembermanagepush.dto.OffLineMemberDTO;
import com.java.test.util.RequestGameServiceUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;


/**
 * 在线会员service
 *
 * @author lijie1
 * @date 2020/7/9 0009
 */
@Slf4j
@Service
public class OnLineMemberManageService {

    @Value("${get.game.offLineUser.url}")
    private String offLineUserUrl;

    @Autowired
    private OnLineMemberManageDao onLineMemberManageDao;

    /***
     * 会员下线
     * @author lijie1
     * @date 2020/7/9 0009
     */
    public ResponseDTO<String> offLineMember(OffLineMemberDTO dto) {
            String time = new Date().getTime()+"";
            String data = "{\"code\":1,\"data\":[\""+dto.getId()+"\"]}";
            Random r1 = new Random();
            String url =  RequestGameServiceUtil.createUrl(time,r1.nextInt(100)+"",data,offLineUserUrl);
            try {
                log.debug(url);
                JSONObject resMsg = RequestGameServiceUtil.getService(url);
                String state = (resMsg.get("state").toString());
                if("0".equals(state)){
                    int i = onLineMemberManageDao.deleteOnLineMember(dto.getId());
                    if(i>0){
                        return ResponseDTO.succ();
                    }else {
                        return ResponseDTO.error("下线数据异常");
                    }

                }else {
                    return ResponseDTO.error(resMsg.get("message").toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
    }

}
